package com.sztx.worker_monitor.web.controller;


import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.sztx.se.common.domain.Result;
import com.sztx.se.common.domain.ResultCode;
import com.sztx.se.web.controller.BaseController;
import com.sztx.worker_monitor.web.handle.CommonHandle;
import com.sztx.worker_monitor.web.handle.TBSDealHandle;
import com.sztx.worker_monitor.web.handle.TBSchedNameHandle;
import com.sztx.worker_monitor.web.model.GlobalSeting;
import com.sztx.worker_monitor.web.model.ResultStatus;
import com.sztx.worker_monitor.web.model.SearchListModel;
import com.sztx.worker_monitor.web.model.TaskRunInfoModel;
import com.sztx.worker_monitor.web.page.TbsIndexPage;


@RequestMapping("/tbschedule")
@Controller
public class TBSController extends BaseController {
	
	@Autowired(required = false)
	private TBSDealHandle tBSDealHandle;
	
	@Autowired(required = false)
	private TBSchedNameHandle tbschedNameHandle;
	
	
	/** *****************************以下为页面控制****************************************/
	@RequestMapping(value="/index", method=RequestMethod.GET)
	public String index(HttpServletRequest request, HttpServletResponse response, Model model) {

		GlobalSeting.setLogonAndMenu(model, GlobalSeting.MenuIndex.Index);
		String keyTaskname =  getStringParameter("key_taskname",""); 
	    String keySchedname = getStringParameter("key_schedname","");
	        
		TbsIndexPage tbsIndexPage = tBSDealHandle.getTbsIndexPage(keyTaskname, keySchedname);
		
		model.addAttribute("tbsIndexPage", tbsIndexPage);
		return "tbschedule/index";
	}
	
	@RequestMapping(value="/about", method=RequestMethod.GET)
	public String about(HttpServletRequest request, HttpServletResponse response, Model model) {

		GlobalSeting.setLogonAndMenu(model, GlobalSeting.MenuIndex.About);
		return "tbschedule/about";
	}
	/** *****************************以上为页面控制****************************************/

	
	/** *****************************以下是post消息****************************************/
	@RequestMapping(value="/addNewJob", method=RequestMethod.POST)
	public Result addNewJob(HttpServletRequest getIntegerParameter, HttpServletResponse response, Model model) {

		String 	scheduleName 	= getStringParameter("scheduleName");
		String 	strategyName 	= getStringParameter("strategyName");
		String 	taskName 		= getStringParameter("taskName");
		String 	taskBean 		= getStringParameter("taskBean");
		Integer fetchDataNumber = getIntegerParameter("fetchDataNumber");
		Integer executeNumber	= getIntegerParameter("executeNumber");
		
		String 	taskItems 		= getStringParameter("taskItems");
		String	startTime 		= getStringParameter("startTime");
		String 	endTime 		= getStringParameter("endTime");  
		String 	extraInfo 		= getStringParameter("extraInfo");   	
		

		ResultStatus resultStatus = tBSDealHandle.addNewJob(	
				scheduleName,
				strategyName,
				taskName,
				taskBean,
				fetchDataNumber,
				executeNumber,
				taskItems,
				startTime,
				endTime,
				extraInfo);
		if(!resultStatus.getbSuccess()){
			Result result = new Result(ResultCode.COMMON_SYSTEM_EXCEPTION, false);
			result.setProperty("errDes", resultStatus.getErrorDes());
			return result;
		}
			
		Result result = new Result(ResultCode.COMMON_SUCCESS, true);
		return result;

	}
	
	@RequestMapping(value="/delJob", method=RequestMethod.POST)
	public Result delJob(HttpServletRequest getIntegerParameter, HttpServletResponse response, Model model) {

		String 	scheduleName 	= getStringParameter("scheduleName");
		String 	strategyName 	= getStringParameter("strategyName");
		String 	taskName 		= getStringParameter("taskName");
		

		ResultStatus resultStatus = tBSDealHandle.delJob(	
				scheduleName,
				strategyName,
				taskName
				);
		if(!resultStatus.getbSuccess()){
			Result result = new Result(ResultCode.COMMON_SYSTEM_EXCEPTION, false);
			result.setProperty("errDes", resultStatus.getErrorDes());
			return result;
		}
			
		Result result = new Result(ResultCode.COMMON_SUCCESS, true);
		return result;

	}
	
	@RequestMapping(value="/pauseJob", method=RequestMethod.POST)
	public Result pauseJob(HttpServletRequest getIntegerParameter, HttpServletResponse response, Model model) {

		String 	scheduleName 	= getStringParameter("scheduleName");
		String 	strategyName 	= getStringParameter("strategyName");
		String 	taskName 		= getStringParameter("taskName");
		

		ResultStatus resultStatus = tBSDealHandle.pauseJob(	
				scheduleName,
				strategyName,
				taskName
				);
		if(!resultStatus.getbSuccess()){
			Result result = new Result(ResultCode.COMMON_SYSTEM_EXCEPTION, false);
			result.setProperty("errDes", resultStatus.getErrorDes());
			return result;
		}
			
		Result result = new Result(ResultCode.COMMON_SUCCESS, true);
		return result;

	}
	
	@RequestMapping(value="/resumeJob", method=RequestMethod.POST)
	public Result resumeJob(HttpServletRequest getIntegerParameter, HttpServletResponse response, Model model) {

		String 	scheduleName 	= getStringParameter("scheduleName");
		String 	strategyName 	= getStringParameter("strategyName");
		String 	taskName 		= getStringParameter("taskName");
		

		ResultStatus resultStatus = tBSDealHandle.resumeJob(	
				scheduleName,
				strategyName,
				taskName
				);
		if(!resultStatus.getbSuccess()){
			Result result = new Result(ResultCode.COMMON_SYSTEM_EXCEPTION, false);
			result.setProperty("errDes", resultStatus.getErrorDes());
			return result;
		}
			
		Result result = new Result(ResultCode.COMMON_SUCCESS, true);
		return result;

	}
	
	@RequestMapping(value="/updateJob", method=RequestMethod.POST)
	public Result updateJob(HttpServletRequest getIntegerParameter, HttpServletResponse response, Model model) {

		String 	scheduleName 	= getStringParameter("scheduleName");
		String 	strategyName 	= getStringParameter("strategyName");
		String 	taskName 		= getStringParameter("taskName");
		String 	taskBean 		= getStringParameter("taskBean");
		Integer fetchDataNumber = getIntegerParameter("fetchDataNumber");
		Integer executeNumber	= getIntegerParameter("executeNumber");
		
		String 	taskItems 		= getStringParameter("taskItems");
		String	startTime 		= getStringParameter("startTime");
		String 	endTime 		= getStringParameter("endTime");  
		String 	extraInfo 		= getStringParameter("extraInfo");   	
		

		ResultStatus resultStatus = tBSDealHandle.updateJob(	
				scheduleName,
				strategyName,
				taskName,
				taskBean,
				fetchDataNumber,
				executeNumber,
				taskItems,
				startTime,
				endTime,
				extraInfo);
		if(!resultStatus.getbSuccess()){
			Result result = new Result(ResultCode.COMMON_SYSTEM_EXCEPTION, false);
			result.setProperty("errDes", resultStatus.getErrorDes());
			return result;
		}
			
		Result result = new Result(ResultCode.COMMON_SUCCESS, true);
		return result;

	}
	
	/**
	 * 删除一个schedname
	 * */
	@RequestMapping(value="/delSchedName", method=RequestMethod.POST)
	public Result delSchedName(HttpServletRequest request, HttpServletResponse response, Model model) {

		String errDes = tbschedNameHandle.delSchedName(getStringParameter("schedName"));
		if(errDes != null){
			Result result = new Result(ResultCode.COMMON_SYSTEM_EXCEPTION, false);
			result.setProperty("errDes", errDes);
			return result;
		}
		Result result = new Result(ResultCode.COMMON_SUCCESS, true);
		return result;
	}
	
	/**
	 * 添加一个schedname
	 * */
	@RequestMapping(value="/addSchedName", method=RequestMethod.POST)
	public Result addSchedName(HttpServletRequest request, HttpServletResponse response, Model model) {

		String errDes = tbschedNameHandle.addSchedName(getStringParameter("schedName"));
		if(errDes != null){
			Result result = new Result(ResultCode.COMMON_SYSTEM_EXCEPTION, false);
			result.setProperty("errDes", errDes);
			return result;
		}
		Result result = new Result(ResultCode.COMMON_SUCCESS, true);
		return result;
	}
	
	/**
	 * 获取所有schedname
	 * */
	@RequestMapping(value="/getAllSchedName", method=RequestMethod.POST)
	public Result getAllSchedName(HttpServletRequest request, HttpServletResponse response, Model model) {


		ResultStatus  resultStatus= tbschedNameHandle.getAllSchedName();

		Result result = new Result(ResultCode.COMMON_SUCCESS, true);
		result.setProperty("schedNames", resultStatus.getRevert());
		return result;
	}
	
	/**
	 * 解析一个expression
	 * ****/
	@RequestMapping(value="/showExDes", method=RequestMethod.POST)
	public Result showExDes(HttpServletRequest request, HttpServletResponse response, Model model) {


		String exdes = CommonHandle.getExDes(getStringParameter("expression"));
		
		Result result = new Result(ResultCode.COMMON_SUCCESS, true);
		result.setProperty("exdes", exdes);
		return result;
	}
	/**
	 * 获取所有的 jobname  和schedulname
	 * 
	 * */
	@RequestMapping(value="/getSearchList", method=RequestMethod.POST)
	public Result getSearchList(HttpServletRequest request, HttpServletResponse response, Model model) {


		SearchListModel searchListModel = tBSDealHandle.getSearchList();
		Result result = new Result(ResultCode.COMMON_SUCCESS, true);
		result.setProperty("searchListModel", searchListModel);
		return result;
	}
	
	
	/**
	 * 获取运行信息
	 * 
	 * */
	@RequestMapping(value="/getTaskRuningInfo", method=RequestMethod.POST)
	public Result getTaskRuningInfo(HttpServletRequest request, HttpServletResponse response, Model model) {

		String 	scheduleName 	= getStringParameter("scheduleName");
		String 	strategyName 	= getStringParameter("strategyName");
		String 	taskName 		= getStringParameter("taskName");

		List<TaskRunInfoModel> taskRunInfoList = tBSDealHandle.getTaskRuningInfo(
					scheduleName,
				 	strategyName,
					taskName);
		Result result = new Result(ResultCode.COMMON_SUCCESS, true);
		result.setProperty("taskRunInfoList", taskRunInfoList);
		return result;
	}
	
	/** *****************************以上是post消息****************************************/

	
	
	
	
}
